package control;

import control.bean.ControlBeanConstant;
import control.bean.ControlPropertiesBean;
import org.omg.CORBA.ContextList;
import ui.FormRow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/19/13
 * Time: 2:05 PM
 * To change this template use File | Settings | File Templates.
 */
public class Textfield extends Control {

    JTextField input;

    public Textfield() {
        fieldType = ControlBeanConstant.TEXTFIELD;
        buildControlPanel();
        buildPropertiesPanel();
    }

    @Override
    public Component buildControlPanel() {
        controlPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        controlPanel.setOpaque(false);

        label = new JTextField(super.labelText);
        Dimension labelD = new Dimension(130, 20);
        label.setPreferredSize(labelD);
        label.setMinimumSize(labelD);

        input = new JTextField("");
        Dimension inputD = new Dimension(190, 20);
        input.setPreferredSize(inputD);
        input.setMinimumSize(inputD);

        controlPanel.addFocusListener(new ControlFocusAdaptor());
        controlPanel.addMouseListener(new ControlMouseAdaptor());

        input.addFocusListener(new ControlFocusAdaptor());
        input.addMouseListener(new ControlMouseAdaptor());
        label.addFocusListener(new ControlFocusAdaptor());
        label.addMouseListener(new ControlMouseAdaptor());

        controlPanel.add(label);
        controlPanel.add(input);
        return controlPanel;
    }

    private JComboBox options;

    private JCheckBox calculatedField;
    private JCheckBox password;

    @Override
    public Component buildPropertiesPanel() {
        propertiesPanel = new JPanel(new GridBagLayout());

        fieldProperties = new JLabel("Field Properties");

        controlLabel = new JLabel("Control Name");
        controlName = new JTextField();


        options = new JComboBox();

        hint = new JCheckBox("Hint");
        required = new JCheckBox("Required");
        calculatedField = new JCheckBox("Calculated Field");
        password = new JCheckBox("Password");
        readOnly = new JCheckBox("Read Only");

        //TODO redo layout using gridbag with control name label
        GridBagConstraints constraints;
        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.ipady = 14;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(fieldProperties, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.ipadx = 0;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(controlLabel, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 1;
        //constraints.gridheight = 2;
//        constraints.ipadx = 114;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(controlName, constraints);
//        controlName.addFocusListener(new FocusAdapter() {
//            @Override
//            public void focusLost(FocusEvent e) {
//                System.out.println(e.getSource());
//                System.out.println("New Value = "+ controlName.getText());
//            }
//        });

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
//        constraints.ipadx = 136;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(5, 0, 0, 0);
        propertiesPanel.add(options, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        propertiesPanel.add(hint, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(required, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(calculatedField, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 7;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(password, constraints);

        constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 8;
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(1, 0, 0, 0);
        propertiesPanel.add(readOnly, constraints);

        Component strut = Box.createHorizontalStrut(160);
        constraints.gridy = 9;
        propertiesPanel.add(strut, constraints);

        return propertiesPanel;
    }

    public ControlPropertiesBean exportData() {
        super.exportData();

        try {
            controlPropertiesBean.addProperty(ControlBeanConstant.INPUTVALUE, input.getText());
        } catch ( Exception e ) {
            System.out.println("Drop down list not set.");
        }

        return controlPropertiesBean;
    }

    public void importData(ControlPropertiesBean controlPropertiesBean) {
        try {
            super.importData(controlPropertiesBean);
            input.setText(controlPropertiesBean.getProperty(ControlBeanConstant.INPUTVALUE));
        } catch (Exception e ) {

        }
    }
}
